package io.renren.modules.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.basic.entity.*;
import io.renren.modules.basic.service.*;
import io.renren.modules.ioserver.IOServerService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.wcs.entity.DDJJobEntity;
import io.renren.modules.wcs.entity.WeightingPLCEntity;
import io.renren.modules.wcs.enums.IOServerEnum;
import io.renren.modules.wcs.service.DDJService;
import io.renren.modules.wcs.service.WeighingPLCService;
import io.renren.modules.wcs.service.impl.WeighingPLCServiceImpl;
import io.renren.modules.websocket.WebSocketServer;
import io.renren.modules.work.Utils.BackWarehous;
import io.renren.modules.work.Utils.BackWarehouseForTaskInfoDet;
import io.renren.modules.work.Utils.ManageConstant;
import io.renren.modules.work.Utils.ManagePublicMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 
 *
 * @author liguanghao
 * @date 2019-11-13 10:50:42
 */
@Api(description = "子任务")
@RestController
@RequestMapping(value = "generator/mtcctaskruninfodet")
public class MtCcTaskRunInfoDetController extends AbstractController {
    @Autowired
    private MtCcTaskRunInfoDetService mtCcTaskRunInfoDetService;
    @Autowired
    private MtCcTaskRunInfoService mtCcTaskRunInfoService;
    @Autowired
    private MtCcWarehousRankService mtCcWarehousRankService;
    @Autowired
    private MtCcWarehousPositionRelatService mtCcWarehousPositionRelatService;
    @Autowired
    private MtCcTaskRunInfoDetLogService mtCcTaskRunInfoDetLogService;
    @Autowired
    private MtCcProductService mtCcProductService;
    @Autowired
    private WeighingPLCService weighingPLCService;
    @Autowired
    private MtCcDeviceCommunLogService mtCcDeviceCommunLogService;
    @Autowired
    private DDJService ddjService;
    @Autowired
    private MtCcWarehousPositionRelatLogService mtCcWarehousPositionRelatLogService;
    @Autowired
    private IOServerService ioServerService;
    @Autowired
    private WebSocketServer webSocketServer;
    private final static Logger logger = LoggerFactory.getLogger(WeighingPLCServiceImpl.class);

    /**
     * 各个主任务的子任务列表
     */
    @ApiOperation(value = "各个主任务的子任务列表列表",notes="各个主任务的子任务列表列表")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        List<MtCcTaskRunInfoDetEntity> list = mtCcTaskRunInfoDetService.queryPage(params);

        return R.ok().put("list", list);
    }

    /**
     * 子任务列表
     */
    @ApiOperation(value = "子任务列表列表",notes="子任务列表列表")
    @RequestMapping("/taskruninfodetlist")
    public R taskRunInfoDetList(){
        List<MtCcTaskRunInfoDetEntity> list = mtCcTaskRunInfoDetService.list(new QueryWrapper<MtCcTaskRunInfoDetEntity>().eq("status",1));

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "信息",notes="信息")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:mtcctaskruninfodet:info")
    public R info(@PathVariable("id") Integer id){
		MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDet = mtCcTaskRunInfoDetService.getById(id);

        return R.ok().put("mtCcTaskRunInfoDet", mtCcTaskRunInfoDet);
    }

    /**
     * 保存多种物料到子任务
     */
    @ApiOperation(value = "保存多种物料到子任务",notes="保存多种物料到子任务")
    @RequestMapping("/save/products")
    @Transactional(rollbackFor = Exception.class)
    public R saveProducts(@RequestBody Map<String, Object> params){
        String productCode = String.valueOf(params.get("productCode"));
        String number = String.valueOf(params.get("number"));
        String remarks = String.valueOf(params.get("remarks"));
        String dateNum = String.valueOf(params.get("dateNum"));

        SysUserEntity user = this.getUser();
        String doorWay = user.getDoorWay();

        //创建主任务
        MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity=new MtCcTaskRunInfoEntity();
        mtCcTaskRunInfoEntity.setCreateTime(new Date());
        mtCcTaskRunInfoEntity.setUserId(Integer.parseInt(String.valueOf(user.getUserId())));
        mtCcTaskRunInfoEntity.setUserName(user.getUsername());
        mtCcTaskRunInfoEntity.setSource(user.getUsername());
        mtCcTaskRunInfoEntity.setTaskType(1);
        mtCcTaskRunInfoEntity.setStatus(0);
        mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE,doorWay));
        mtCcTaskRunInfoEntity.setProductId(0);
        mtCcTaskRunInfoEntity.setModel("混合物料");
        mtCcTaskRunInfoEntity.setSpecModel("混合物料");
        mtCcTaskRunInfoEntity.setUnit("混合");
        mtCcTaskRunInfoEntity.setNum("混合");
        mtCcTaskRunInfoEntity.setRemarks("多种物料");
        mtCcTaskRunInfoEntity.setDateNum("多种物料");
        mtCcTaskRunInfoService.save(mtCcTaskRunInfoEntity);

        MtCcTaskRunInfoEntity mtCcTaskRunInfoServiceOne = mtCcTaskRunInfoService
                .getOne(new QueryWrapper<MtCcTaskRunInfoEntity>()
                        .eq("task_code", mtCcTaskRunInfoEntity.getTaskCode()));

        //创建子任务
        MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity=new MtCcTaskRunInfoDetEntity();
        mtCcTaskRunInfoDetEntity.setCreateTime(new Date());
        mtCcTaskRunInfoDetEntity.setTaskType(1);
        mtCcTaskRunInfoDetEntity.setStatus(0);
        mtCcTaskRunInfoDetEntity.setTaskRunId(mtCcTaskRunInfoServiceOne.getId());
        mtCcTaskRunInfoDetEntity.setTaskDetCode(mtCcTaskRunInfoServiceOne.getTaskCode());
        mtCcTaskRunInfoDetEntity.setModel(productCode);
        mtCcTaskRunInfoDetEntity.setNum(number);
        mtCcTaskRunInfoDetEntity.setRemarks(remarks);
        mtCcTaskRunInfoDetEntity.setDateNum(dateNum);
        //自动分配库位
        if (mtCcTaskRunInfoDetEntity.getModel().substring(0, 1).equals("3")||mtCcTaskRunInfoDetEntity.getModel().substring(0, 1).equals("4")) {//BYH系列指定第8层
            List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                    .list(new QueryWrapper<MtCcWarehousRankEntity>()
                            .between("id", 241, 320)
                            .eq("is_occupy", 0)
                            .orderByAsc("id"));
            if (list.size() == 0) {
                return R.error("立体库7,8层已满,无法存入货物!");
            } else {
                MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
                mtCcWarehousRankEntity.setIsOccupy(1);
                mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
                mtCcTaskRunInfoDetEntity.setWarehousRankId(mtCcWarehousRankEntity.getId());
                mtCcTaskRunInfoDetEntity.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

            }
        }
        if (mtCcTaskRunInfoDetEntity.getModel().substring(0, 1).equals("2")||mtCcTaskRunInfoDetEntity.getModel().substring(0, 1).equals("t")) {//从1层开始往上
            List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                    .list(new QueryWrapper<MtCcWarehousRankEntity>()
                            .between("id", 1, 320)
                            .eq("is_occupy", 0)
                            .orderByAsc("id"));
            if (list.size() == 0) {//长度为0，立体库已满
                return R.error("立体库已满,无法存入货物!");
            } else {
                MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
                mtCcWarehousRankEntity.setIsOccupy(1);
                mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
                mtCcTaskRunInfoDetEntity.setWarehousRankId(mtCcWarehousRankEntity.getId());
                mtCcTaskRunInfoDetEntity.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

            }

        }
        if (mtCcTaskRunInfoDetEntity.getModel().substring(0,1).equals("0")){
            List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                    .list(new QueryWrapper<MtCcWarehousRankEntity>()
                            .between("id", 321, 335)
                            .eq("is_occupy", 0)
                            .orderByAsc("id"));
            if (list.size() == 0) {//长度为0，托盘专用库位已满
                list = mtCcWarehousRankService
                        .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                .between("id", 1, 320)
                                .eq("is_occupy", 0)
                                .orderByAsc("id"));
            }
            if (list.size() == 0) {
                return R.error("立体库已满,无法存入货物!");
            }
            MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
            mtCcWarehousRankEntity.setIsOccupy(1);
            mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
            mtCcTaskRunInfoDetEntity.setWarehousRankId(mtCcWarehousRankEntity.getId());
            mtCcTaskRunInfoDetEntity.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

        }
        if (mtCcTaskRunInfoDetEntity.getModel().substring(0, 1).equals("1")) {//56层然后往下
            List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                    .list(new QueryWrapper<MtCcWarehousRankEntity>()
                            .between("id", 161, 240)
                            .eq("is_occupy", 0)
                            .orderByAsc("id"));
            if (list.size() == 0) {//长度为0，56层已满
                list = mtCcWarehousRankService
                        .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                .between("id", 1, 160)
                                .eq("is_occupy", 0)
                                .orderByDesc("id"));
            }
            if (list.size() == 0) {//立体库已满
                return R.error("立体库已满,无法存入货物!");
            } else {
                MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
                mtCcWarehousRankEntity.setIsOccupy(1);
                mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
                mtCcTaskRunInfoDetEntity.setWarehousRankId(mtCcWarehousRankEntity.getId());
                mtCcTaskRunInfoDetEntity.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

            }

        }
        mtCcTaskRunInfoDetService.save(mtCcTaskRunInfoDetEntity);
        int lengthForCode = mtCcTaskRunInfoDetEntity.getModel().length();//编码长度
        int productNumber=(lengthForCode+1)/10;//该库位上存放的产品有几种
        for (int i = 0; i <productNumber ; i++) {
            String code = mtCcTaskRunInfoDetEntity.getModel().substring(10 * i, 10 * i + 9);
            String[] strings = mtCcTaskRunInfoDetEntity.getNum().split("\\+");
            String productQuantity=strings[i];//第i种货物的数量
            String[] productRemarks=mtCcTaskRunInfoDetEntity.getRemarks().split("\\+");
            String productRemark=productRemarks[i];//第i种货物的备注
            String[] productDateNums=mtCcTaskRunInfoDetEntity.getDateNum().split("\\+");
            String productDateNum=productDateNums[i];//第i种货物的批号
            MtCcProductEntity product = mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>().eq("product_code", code));
            //插入对应库位状态为被占用、货物预入库
            MtCcWarehousPositionRelatEntity mtCcwarehousPositionRelate = new MtCcWarehousPositionRelatEntity();
            mtCcwarehousPositionRelate.setCreateTime(mtCcTaskRunInfoDetEntity.getCreateTime());
            mtCcwarehousPositionRelate.setStatus(3);
            mtCcwarehousPositionRelate.setIsOccupy(1);
            mtCcwarehousPositionRelate.setProductId(product.getId());
            mtCcwarehousPositionRelate.setProductCode(product.getProductCode());
            mtCcwarehousPositionRelate.setProductName(product.getProductName());
            mtCcwarehousPositionRelate.setSpecModel(product.getSpecModel());
            mtCcwarehousPositionRelate.setQuantity(Integer.parseInt(productQuantity));
            mtCcwarehousPositionRelate.setWarehousPositionId(mtCcTaskRunInfoDetEntity.getWarehousRankId());
            mtCcwarehousPositionRelate.setWarehousPositionName(mtCcWarehousRankService.getById(mtCcTaskRunInfoDetEntity.getWarehousRankId()).getAnotherName());
            mtCcwarehousPositionRelate.setDateNum(productDateNum);
            mtCcwarehousPositionRelate.setRemark(productRemark);
            mtCcWarehousPositionRelatService.save(mtCcwarehousPositionRelate);
            MtCcWarehousRankEntity mtCcWarehousRank = mtCcWarehousRankService.getOne(new QueryWrapper<MtCcWarehousRankEntity>()
                    .eq("id", mtCcwarehousPositionRelate.getWarehousPositionId()));
            mtCcWarehousRank.setIsOccupy(1);
            mtCcWarehousRankService.updateById(mtCcWarehousRank);//更新rank表里库位状态
        }
        return R.ok();
    }


    /**
     * 保存
     */
    @ApiOperation(value = "保存",notes="保存")
    @RequestMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody Map<String, Object> params){
        int taskInfoId = Integer.parseInt(String.valueOf(params.get("id")));
        MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity = mtCcTaskRunInfoService.getById(taskInfoId);
        String doorWay=mtCcTaskRunInfoEntity.getTaskCode().substring(1,3);
        String taskCode = ManagePublicMethod.creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE, doorWay);
        MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDet=new MtCcTaskRunInfoDetEntity();
        mtCcTaskRunInfoDet.setTaskRunId(mtCcTaskRunInfoEntity.getId());
        mtCcTaskRunInfoDet.setTaskType(mtCcTaskRunInfoEntity.getTaskType());
        mtCcTaskRunInfoDet.setTaskDetCode(taskCode);
        mtCcTaskRunInfoDet.setModel(mtCcTaskRunInfoEntity.getModel());
        mtCcTaskRunInfoDet.setUnit(mtCcTaskRunInfoEntity.getUnit());
        mtCcTaskRunInfoDet.setDateNum(mtCcTaskRunInfoEntity.getDateNum());
        mtCcTaskRunInfoDet.setRemarks(mtCcTaskRunInfoEntity.getRemarks());

        Integer warehousRankId = Integer.parseInt(String.valueOf(params.get("warehousRankId")));
        String productModel="";
        String productNum="";
        String productUnit="";
        String remarks="";
        String datenum="";
        if (warehousRankId==0) {//库位id为0代表自动分配
            if (mtCcTaskRunInfoDet.getModel().substring(0, 1).equals("3")||mtCcTaskRunInfoDet.getModel().substring(0, 1).equals("4")) {//BYH系列指定第8层
                List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                        .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                .between("id", 241, 320)
                                .eq("is_occupy", 0)
                                .orderByAsc("id"));
                if (list.size() == 0) {
                    return R.error("立体库7,8层已满,无法存入货物!");
                } else {
                    MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
                    mtCcWarehousRankEntity.setIsOccupy(1);
                    mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
                    mtCcTaskRunInfoDet.setWarehousRankId(mtCcWarehousRankEntity.getId());
                    mtCcTaskRunInfoDet.setNum(String.valueOf(params.get("num")));
                    mtCcTaskRunInfoDet.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

                }
            }
            if (mtCcTaskRunInfoDet.getModel().substring(0, 1).equals("2")||mtCcTaskRunInfoDet.getModel().substring(0, 1).equals("t")) {//从1层开始往上
                List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                        .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                .between("id", 1, 320)
                                .eq("is_occupy", 0)
                                .orderByAsc("id"));
                if (list.size() == 0) {//长度为0，立体库已满
                    return R.error("立体库已满,无法存入货物!");
                } else {
                    MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
                    mtCcWarehousRankEntity.setIsOccupy(1);
                    mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
                    mtCcTaskRunInfoDet.setWarehousRankId(mtCcWarehousRankEntity.getId());
                    mtCcTaskRunInfoDet.setNum(String.valueOf(params.get("num")));
                    mtCcTaskRunInfoDet.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

                }

            }
            if (mtCcTaskRunInfoDet.getModel().substring(0,1).equals("0")){
                List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                        .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                .between("id", 321, 335)
                                .eq("is_occupy", 0)
                                .orderByAsc("id"));
                if (list.size() == 0) {//长度为0，托盘专用库位已满
                    list = mtCcWarehousRankService
                            .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                    .between("id", 1, 320)
                                    .eq("is_occupy", 0)
                                    .orderByAsc("id"));
                }
                if (list.size() == 0) {
                    return R.error("立体库已满,无法存入货物!");
                    }
                    MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
                    mtCcWarehousRankEntity.setIsOccupy(1);
                    mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
                    mtCcTaskRunInfoDet.setWarehousRankId(mtCcWarehousRankEntity.getId());
                    mtCcTaskRunInfoDet.setNum(String.valueOf(params.get("num")));
                    mtCcTaskRunInfoDet.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

                }

            if (mtCcTaskRunInfoDet.getModel().substring(0, 1).equals("1")) {//56层然后往下
                List<MtCcWarehousRankEntity> list = mtCcWarehousRankService
                        .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                .between("id", 161, 240)
                                .eq("is_occupy", 0)
                                .orderByAsc("id"));
                if (list.size() == 0) {//长度为0，56层已满
                    list = mtCcWarehousRankService
                            .list(new QueryWrapper<MtCcWarehousRankEntity>()
                                    .between("id", 1, 160)
                                    .eq("is_occupy", 0)
                                    .orderByDesc("id"));
                }
                if (list.size() == 0) {//立体库已满
                    return R.error("立体库已满,无法存入货物!");
                } else {
                    MtCcWarehousRankEntity mtCcWarehousRankEntity = list.get(0);
                    mtCcWarehousRankEntity.setIsOccupy(1);
                    mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新库位状态为被占用
                    mtCcTaskRunInfoDet.setWarehousRankId(mtCcWarehousRankEntity.getId());
                    mtCcTaskRunInfoDet.setNum(String.valueOf(params.get("num")));
                    mtCcTaskRunInfoDet.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());

                }

            }
            mtCcTaskRunInfoDet.setCreateTime(mtCcTaskRunInfoEntity.getCreateTime());
            mtCcTaskRunInfoDet.setStatus(0);
       /* List<MtCcTaskRunInfoDetEntity> mtCcTaskRunInfoDetEntityList = mtCcTaskRunInfoDetService.list(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                .eq("warehous_rank_id", mtCcTaskRunInfoDet.getWarehousRankId())
                .eq("task_run_id", mtCcTaskRunInfoDet.getTaskRunId()));//同一任务下是否有相同位置的出库任务，若有，本条子任务状态置为"4"(待确认执行)
        if (mtCcTaskRunInfoDetEntityList!=null){
            mtCcTaskRunInfoDet.setModel(productModel.concat(mtCcTaskRunInfoDet.getModel()));
            mtCcTaskRunInfoDet.setNum(productNum.concat(mtCcTaskRunInfoDet.getNum()));
            mtCcTaskRunInfoDet.setUnit(productUnit.concat(mtCcTaskRunInfoDet.getUnit()));
            mtCcTaskRunInfoDet.setStatus(4);
        }*/
            mtCcTaskRunInfoDetService.save(mtCcTaskRunInfoDet);
            //根据产品code查询产品id、name
            MtCcProductEntity product = mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>()
                    .eq("product_code", mtCcTaskRunInfoDet.getModel()));
            //插入对应库位状态为被占用、货物预入库
            MtCcWarehousPositionRelatEntity mtCcwarehousPositionRelate = new MtCcWarehousPositionRelatEntity();
            mtCcwarehousPositionRelate.setCreateTime(mtCcTaskRunInfoDet.getCreateTime());
            mtCcwarehousPositionRelate.setStatus(3);
            mtCcwarehousPositionRelate.setIsOccupy(1);
            mtCcwarehousPositionRelate.setProductId(product.getId());
            mtCcwarehousPositionRelate.setProductCode(product.getProductCode());
            mtCcwarehousPositionRelate.setProductName(product.getProductName());
            mtCcwarehousPositionRelate.setSpecModel(product.getSpecModel());
            mtCcwarehousPositionRelate.setQuantity(Integer.parseInt(String.valueOf(params.get("num"))));
            mtCcwarehousPositionRelate.setWarehousPositionId(mtCcTaskRunInfoDet.getWarehousRankId());
            mtCcwarehousPositionRelate.setWarehousPositionName(mtCcWarehousRankService.getById(mtCcTaskRunInfoDet.getWarehousRankId()).getAnotherName());
            mtCcwarehousPositionRelate.setDateNum(mtCcTaskRunInfoDet.getDateNum());
            mtCcwarehousPositionRelate.setRemark(mtCcTaskRunInfoDet.getRemarks());
            mtCcWarehousPositionRelatService.save(mtCcwarehousPositionRelate);
            MtCcWarehousRankEntity mtCcWarehousRank = mtCcWarehousRankService.getOne(new QueryWrapper<MtCcWarehousRankEntity>()
                    .eq("id", mtCcwarehousPositionRelate.getWarehousPositionId()));
            mtCcWarehousRank.setIsOccupy(1);
            mtCcWarehousRankService.updateById(mtCcWarehousRank);//更新rank表里库位状态
            return R.ok();
        }else{//库位id不为0，代表手动分配库位
            //查询该库位原本是否被占用
            MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankService
                    .getOne(new QueryWrapper<MtCcWarehousRankEntity>()
                            .eq("id", warehousRankId));
            if (mtCcWarehousRankEntity.getIsOccupy()==1){//已经被占用
                List<MtCcWarehousPositionRelatEntity> mtCcWarehousPositionRelats = mtCcWarehousPositionRelatService.list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                        .eq("warehous_position_id", warehousRankId));//查找出仓库内对应位置存放的货物列表

                for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:mtCcWarehousPositionRelats){
                    //更新库位状态为货物预出库
                    mtCcWarehousPositionRelatEntity.setStatus(4);
                    mtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelatEntity);
                    MtCcProductEntity productOnWarehousPositionRelat = mtCcProductService.getById(mtCcWarehousPositionRelatEntity.getProductId());//当前库位上货物的信息
                    productModel=productModel.concat(productOnWarehousPositionRelat.getProductCode()).concat("+");//每种货物编码后加“+”区分
                    productNum=productNum.concat(String.valueOf(mtCcWarehousPositionRelatEntity.getQuantity()).concat("+"));//每种货物数量后加“+”区分
                    //productUnit=productUnit.concat(productOnWarehousPositionRelat.getUnitName()).concat("+");//每种货物单位名称后加“+”区分
                    remarks=remarks.concat(mtCcWarehousPositionRelatEntity.getRemark()).concat("+");//每种货物备注后加"+"区分
                    datenum=datenum.concat(mtCcWarehousPositionRelatEntity.getDateNum()).concat("+");//每种货物批号后加"+"区分
                }
                //新建一条出库子任务
                MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity=new MtCcTaskRunInfoDetEntity();
                mtCcTaskRunInfoDetEntity.setCreateTime(new Date());
                mtCcTaskRunInfoDetEntity.setWarehousRankId(mtCcWarehousRankEntity.getId());
                mtCcTaskRunInfoDetEntity.setWarehousRankName(mtCcWarehousRankEntity.getAnotherName());
                mtCcTaskRunInfoDetEntity.setTaskRunId(mtCcTaskRunInfoEntity.getId());
                mtCcTaskRunInfoDetEntity.setTaskType(0);
                mtCcTaskRunInfoDetEntity.setStatus(0);
                mtCcTaskRunInfoDetEntity.setTaskDetCode(ManagePublicMethod.creatOrderCode(ManageConstant.OUT_STOCK_ORDER_TYPE,doorWay));
                mtCcTaskRunInfoDetEntity.setModel(productModel);
                mtCcTaskRunInfoDetEntity.setNum(productNum);
                mtCcTaskRunInfoDetEntity.setUnit(productUnit);
                mtCcTaskRunInfoDetEntity.setIsHeight("0");
                mtCcTaskRunInfoDetEntity.setIsWeight("0");
                mtCcTaskRunInfoDetEntity.setRemarks(remarks);
                mtCcTaskRunInfoDetEntity.setDateNum(datenum);
                mtCcTaskRunInfoDetService.save(mtCcTaskRunInfoDetEntity);
            }
            mtCcTaskRunInfoDet.setNum(productNum.concat(String.valueOf(params.get("num"))));
            mtCcTaskRunInfoDet.setWarehousRankId(warehousRankId);
            mtCcTaskRunInfoDet.setWarehousRankName(mtCcWarehousRankService.getById(warehousRankId).getAnotherName());
            mtCcTaskRunInfoDet.setModel(productModel.concat(mtCcTaskRunInfoEntity.getModel()).concat("+"));
            mtCcTaskRunInfoDet.setUnit(mtCcTaskRunInfoEntity.getUnit());
            mtCcTaskRunInfoDet.setDateNum(datenum.concat(mtCcTaskRunInfoEntity.getDateNum()).concat("+"));
            mtCcTaskRunInfoDet.setRemarks(remarks.concat(mtCcTaskRunInfoEntity.getRemarks()).concat("+"));
            mtCcTaskRunInfoDet.setCreateTime(mtCcTaskRunInfoEntity.getCreateTime());
            mtCcTaskRunInfoDet.setStatus(0);
            mtCcTaskRunInfoDetService.save(mtCcTaskRunInfoDet);

            int lengthForCode = mtCcTaskRunInfoDet.getModel().length();//编码长度
            int productNumber=(lengthForCode+1)/10;//该库位上存放的产品有几种
            for (int i = 0; i <productNumber ; i++) {
                String productCode = mtCcTaskRunInfoDet.getModel().substring(10 * i, 10 * i + 9);
                String[] strings = mtCcTaskRunInfoDet.getNum().split("\\+");
                String productQuantity=strings[i];//第i种货物的数量
                String[] productRemarks=mtCcTaskRunInfoDet.getRemarks().split("\\+");
                String productRemark=productRemarks[i];//第i种货物的备注
                String[] productDateNums=mtCcTaskRunInfoDet.getDateNum().split("\\+");
                String productDateNum=productDateNums[i];//第i种货物的批号
                MtCcProductEntity product = mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>().eq("product_code", productCode));
                //插入对应库位状态为被占用、货物预入库
                MtCcWarehousPositionRelatEntity mtCcwarehousPositionRelate = new MtCcWarehousPositionRelatEntity();
                mtCcwarehousPositionRelate.setCreateTime(mtCcTaskRunInfoDet.getCreateTime());
                mtCcwarehousPositionRelate.setStatus(3);
                mtCcwarehousPositionRelate.setIsOccupy(1);
                mtCcwarehousPositionRelate.setProductId(product.getId());
                mtCcwarehousPositionRelate.setProductCode(product.getProductCode());
                mtCcwarehousPositionRelate.setProductName(product.getProductName());
                mtCcwarehousPositionRelate.setSpecModel(product.getSpecModel());
                mtCcwarehousPositionRelate.setQuantity(Integer.parseInt(productQuantity));
                mtCcwarehousPositionRelate.setWarehousPositionId(mtCcTaskRunInfoDet.getWarehousRankId());
                mtCcwarehousPositionRelate.setWarehousPositionName(mtCcWarehousRankService.getById(mtCcTaskRunInfoDet.getWarehousRankId()).getAnotherName());
                mtCcwarehousPositionRelate.setDateNum(productDateNum);
                mtCcwarehousPositionRelate.setRemark(productRemark);
                mtCcWarehousPositionRelatService.save(mtCcwarehousPositionRelate);
                MtCcWarehousRankEntity mtCcWarehousRank = mtCcWarehousRankService.getOne(new QueryWrapper<MtCcWarehousRankEntity>()
                        .eq("id", mtCcwarehousPositionRelate.getWarehousPositionId()));
                mtCcWarehousRank.setIsOccupy(1);
                mtCcWarehousRankService.updateById(mtCcWarehousRank);//更新rank表里库位状态
            }
            return R.ok();
        }
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改",notes="修改")
    @RequestMapping("/update")
    @RequiresPermissions("generator:mtcctaskruninfodet:update")
    public R update(@RequestBody MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDet){
		mtCcTaskRunInfoDetService.updateById(mtCcTaskRunInfoDet);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除",notes="删除")
    @RequestMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Integer id){
        //根据id查找与之关联（同一对应库位的两条出入库任务）的子任务,一并删除
        MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity = mtCcTaskRunInfoDetService.getById(id);
        if (mtCcTaskRunInfoDetEntity.getTaskType()==0){//如果删除的是出库子任务，查找与之关联的那一条入库子任务，//一并删除同时更新日志,同时修改库位状态。
           /* MtCcTaskRunInfoDetEntity inTaskRunInfoDet = mtCcTaskRunInfoDetService.getOne(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                    .eq("task_run_id", mtCcTaskRunInfoDetEntity.getTaskRunId())
                    .eq("warehous_rank_id", mtCcTaskRunInfoDetEntity.getWarehousRankId())
                    .eq("task_type", 1));*/
            MtCcWarehousPositionRelatEntity positionrelat = mtCcWarehousPositionRelatService
                    .getOne(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                            .eq("warehous_position_id", mtCcTaskRunInfoDetEntity.getWarehousRankId()).eq("status",4));
            positionrelat.setStatus(2);
            mtCcWarehousPositionRelatService.updateById(positionrelat);
            mtCcTaskRunInfoDetService.removeById(id);//删除出库子任务

            /*if (inTaskRunInfoDet!=null){
                mtCcTaskRunInfoDetService.removeById(inTaskRunInfoDet.getId());//删除入库子任务
            }
*/

            /*MtCcTaskRunInfoDetLogEntity mtCcTaskRunInfoDetLog = mtCcTaskRunInfoDetLogService
                    .getOne(new QueryWrapper<MtCcTaskRunInfoDetLogEntity>()
                    .eq("task_det_code", mtCcTaskRunInfoDetEntity.getTaskDetCode()));
            MtCcTaskRunInfoDetLogEntity inTaskRunInfoDetLog = mtCcTaskRunInfoDetLogService
                    .getOne(new QueryWrapper<MtCcTaskRunInfoDetLogEntity>()
                            .eq("task_det_code", inTaskRunInfoDet.getTaskDetCode()));
            inTaskRunInfoDetLog.setStatus(3);
            mtCcTaskRunInfoDetLogService.updateById(inTaskRunInfoDetLog);
            mtCcTaskRunInfoDetLog.setStatus(3);
            mtCcTaskRunInfoDetLogService.updateById(mtCcTaskRunInfoDetLog);//更新子任务日志*/

            /*List<MtCcWarehousPositionRelatEntity> warehous_position_id = mtCcWarehousPositionRelatService
                    .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                            .eq("warehous_position_id", mtCcTaskRunInfoDetEntity.getWarehousRankId()));
            for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:warehous_position_id
                 ) {
                mtCcWarehousPositionRelatEntity.setStatus(2);
                mtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelatEntity);//更新库位详情信息
            }*/

        }else {//如果删除的是入库子任务，查找是否有与之关联的出库子任务
            MtCcTaskRunInfoDetEntity outTaskRunInfoDet = mtCcTaskRunInfoDetService.getOne(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                    .eq("task_run_id", mtCcTaskRunInfoDetEntity.getTaskRunId())
                    .eq("warehous_rank_id", mtCcTaskRunInfoDetEntity.getWarehousRankId())
                    .eq("task_type", 0));
            if (outTaskRunInfoDet!=null){//如果有，一并删除
                mtCcTaskRunInfoDetService.removeById(id);//删除入库子任务
                mtCcTaskRunInfoDetService.removeById(outTaskRunInfoDet.getId());//删除出库子任务
                List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatService
                        .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                .eq("warehous_position_id", mtCcTaskRunInfoDetEntity.getWarehousRankId()));
                for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list
                     ) {
                    if (mtCcWarehousPositionRelatEntity.getStatus()==3){
                        mtCcWarehousPositionRelatService.removeById(mtCcWarehousPositionRelatEntity.getId());
                    }
                    if (mtCcWarehousPositionRelatEntity.getStatus()==4){
                        mtCcWarehousPositionRelatEntity.setStatus(2);
                        mtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelatEntity);//更新库位详情信息
                    }

                }

            }else{//没有与之关联的
                mtCcTaskRunInfoDetService.removeById(id);//删除入库子任务
                List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatService
                        .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                .eq("warehous_position_id", mtCcTaskRunInfoDetEntity.getWarehousRankId()).eq("status",3));
                for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity :list
                        ) {
                    mtCcWarehousPositionRelatService.removeById(mtCcWarehousPositionRelatEntity);
                }

                //mtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelat);//更新库位详情信息
                MtCcWarehousRankEntity mtCcWarehousRank = mtCcWarehousRankService
                        .getOne(new QueryWrapper<MtCcWarehousRankEntity>()
                                .eq("id", mtCcTaskRunInfoDetEntity.getWarehousRankId()));
                mtCcWarehousRank.setIsOccupy(0);
                mtCcWarehousRankService.updateById(mtCcWarehousRank);//更新rank表里库位状态
            }
        }

        return R.ok();
    }

    /**
     * 执行子任务
     */
    @ApiOperation(value = "执行子任务",notes="执行子任务")
    @RequestMapping("/runTaskInfoDet/{id}")
    public R runTaskInfoDet(@PathVariable("id") Integer id) throws Exception {
        Boolean isProducts=false;//是否是多物料回库回库过的任务标志位.
        MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity = mtCcTaskRunInfoDetService.getById(id);
        int count = mtCcTaskRunInfoDetService.count(new QueryWrapper<MtCcTaskRunInfoDetEntity>().eq("status", 1));
        if (count>0){
            return R.error("当前存在正在执行的子任务,请稍后重试...");
        }else {
            MtCcTaskRunInfoEntity taskRunInfoServiceById = mtCcTaskRunInfoService.getById(mtCcTaskRunInfoDetEntity.getTaskRunId());
            //出库任务,调用堆垛机之前调用称重模块
            if (mtCcTaskRunInfoDetEntity.getTaskType()==1){
                String doorWay=mtCcTaskRunInfoDetEntity.getTaskDetCode().substring(1,3);
                if (doorWay.equals("A1")){
                    doorWay="0001";
                }
                if (doorWay.equals("B1")){
                    doorWay="0002";
                }
                if (doorWay.equals("A2")){
                    doorWay="0003";
                }
                if (doorWay.equals("A3")){
                    doorWay="0004";
                }
                if (doorWay.equals("A4")){
                    doorWay="0005";
                }
                Integer warehousingBegin = weighingPLCService.warehousingBegin(1, Integer.parseInt(doorWay));
                /*try {
                    Thread.sleep(400);
                }catch (Exception e){}*/

/*                //操作写入日志表mtCcDeviceCommunLog
                MtCcDeviceCommunLogEntity mtCcDeviceCommunLog=new MtCcDeviceCommunLogEntity();
                mtCcDeviceCommunLog.setCreateTime(new Date());
                mtCcDeviceCommunLog.setStatus(0);
                mtCcDeviceCommunLog.setContent("任务编码："+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"启动出入口"+doorWay+"的称重模块");
                mtCcDeviceCommunLogService.save(mtCcDeviceCommunLog);*/
                System.err.println("========="+warehousingBegin+"======");
                if (warehousingBegin==0){//写入成功，启动称重模块
                    WeightingPLCEntity cargoAllMessage =new WeightingPLCEntity();
                    if (mtCcTaskRunInfoDetEntity.getTaskType()==1){//入库1，出库2
                        cargoAllMessage = weighingPLCService.getCargoAllMessage(Integer.parseInt(doorWay),1);
                    }
                    //设定额定重量和高度，计算是否超高超重
                    Integer status = cargoAllMessage.getStatus();//称重模块称重后返回的状态值
                    System.err.println("========="+status+"======");
                    if (status==0){//称重成功
                        Float weight1 = cargoAllMessage.getWeight();//重量报警
                        Float weight = cargoAllMessage.getWeight();//重量
                        Short height = cargoAllMessage.getHeight();//高度

                        logger.info("任务编码:"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"重量:"+weight+"-高度"+height);
                        //设定额定高度和重量，比对---1000KG
                        if(mtCcTaskRunInfoDetEntity.getWarehousRankId()<241&&height==2){
                            webSocketServer.sendtoAll("货物高度超出规定值,请调整后重试!");
                            return R.error("货物高度超出规定值,请调整后重试!");
                        }
                        if (weight1==1){
                            webSocketServer.sendtoAll("货物重量超出1000千克,请减轻重量后重试!");
                            //添加变量用于当称重正常仅高货不能放入一到六层时使用
                            String end = cargoAllMessage.getEnd();
                            String lstart = cargoAllMessage.getLstart();
                            try {
                                //L_Start 和 end 不能同时为true
                                Integer integer1 = ioServerService.setTagValue(lstart, "0", 4);
                                Thread.sleep(100);
                                Integer integer = ioServerService.setTagValue(end, "1", 4);
                                if(integer != 0 && integer1 !=0){
                                    logger.error("时间"+new Date()+",称重模块：1.称重出现异常的时候；2.IOServer写入失败");
                                }
                            }catch (Exception e){
                                //todo 修改
                                logger.error("时间"+new Date()+",称重模块：1.称重出现异常的时候；2.IOServer写入异常");
                            }


                            return R.error("货物重量超出1000千克,请减轻重量后重试!");
                        }
                    } else if(status==2){

                        MtCcDeviceCommunLogEntity mtCcDeviceCommunLogEntity=new MtCcDeviceCommunLogEntity();
                        mtCcDeviceCommunLogEntity.setCreateTime(new Date());
                        mtCcDeviceCommunLogEntity.setStatus(1);
                        mtCcDeviceCommunLogEntity.setContent("称重模块传感器异常，称重失败！"+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"出入口"+doorWay);
                        mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogEntity);
                        Float weight1 = cargoAllMessage.getWeight();//重量报警
                        Float weight = cargoAllMessage.getWeight();//重量
                        Short height = cargoAllMessage.getHeight();//高度

                        logger.info("任务编码:"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"重量:"+weight+"-高度"+height);
                        //设定额定高度和重量，比对---1000KG
                        if(mtCcTaskRunInfoDetEntity.getWarehousRankId()<241&&height==2){
                            webSocketServer.sendtoAll("货物高度超出规定值,请调整后重试!");
                            return R.error("货物高度超出规定值,请调整后重试!");
                        }
                        if (weight1==1){
                            webSocketServer.sendtoAll("货物重量超出1000千克,请减轻重量后重试!");
                            return R.error("货物重量超出1000千克,请减轻重量后重试!");
                        }
                        webSocketServer.sendtoAll("平台上货物位置偏差报警,请整理货物后重试");
                        return R.error("平台上货物位置偏差报警,请整理货物后重试!");
                    } else if (status==1){
                        MtCcDeviceCommunLogEntity mtCcDeviceCommunLogEntity=new MtCcDeviceCommunLogEntity();
                        mtCcDeviceCommunLogEntity.setCreateTime(new Date());
                        mtCcDeviceCommunLogEntity.setStatus(1);
                        mtCcDeviceCommunLogEntity.setContent("称重模块传感器异常，称重失败！"+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"出入口"+doorWay);
                        mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogEntity);
                        Float weight1 = cargoAllMessage.getWeight();//重量报警
                        Float weight = cargoAllMessage.getWeight();//重量
                        Short height = cargoAllMessage.getHeight();//高度

                        logger.info("任务编码:"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"重量:"+weight+"-高度"+height);
                        //设定额定高度和重量，比对---1000KG
                        if(mtCcTaskRunInfoDetEntity.getWarehousRankId()<241&&height==2){
                            webSocketServer.sendtoAll("货物高度超出规定值,请调整后重试!");
                            return R.error("货物高度超出规定值,请调整后重试!");
                        }
                        if (weight1==1){
                            webSocketServer.sendtoAll("货物重量超出1000千克,请减轻重量后重试!");
                            return R.error("货物重量超出1000千克,请减轻重量后重试!");
                        }

                        webSocketServer.sendtoAll("平台上托盘位置偏差报警,请调整位置后重试!");

                        return R.error("称重模块传感器异常，称重失败！"+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"出入口"+doorWay);
                    }else {
                        webSocketServer.sendtoAll("称重模块异常，称重失败！");

                        return R.error("称重模块异常，称重失败！"+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"出入口"+doorWay);
                    }
                }else {
                    //写入日志表
                    MtCcDeviceCommunLogEntity mtCcDeviceCommunLogEntity=new MtCcDeviceCommunLogEntity();
                    mtCcDeviceCommunLogEntity.setCreateTime(new Date());
                    mtCcDeviceCommunLogEntity.setStatus(0);
                    mtCcDeviceCommunLogEntity.setContent("任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"称重模块启动失败，IOServer写入异常");
                    mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogEntity);
                    return R.error("称重模块启动失败，请排除故障后重试！");
                }
            }
            //更改子任务状态为正在执行
            mtCcTaskRunInfoDetEntity.setStatus(1);
            mtCcTaskRunInfoDetService.updateById(mtCcTaskRunInfoDetEntity);
            Integer taskType = mtCcTaskRunInfoDetEntity.getTaskType();
            String taskDetCode = mtCcTaskRunInfoDetEntity.getTaskDetCode();
            String doorWay=taskDetCode.substring(1,3);
            if (doorWay.equals("A1")){
                doorWay="0001";
            }
            if (doorWay.equals("A2")){
                doorWay="0003";
            }
            if (doorWay.equals("A3")){
                doorWay="0004";
            }
            if (doorWay.equals("A4")){
                doorWay="0005";
            }
            if (doorWay.equals("B1")){
                doorWay="0002";
            }
            Integer warehousRankId = mtCcTaskRunInfoDetEntity.getWarehousRankId();
            String position = String.valueOf(warehousRankId + 1000);
            DDJJobEntity ddjJobEntity=new DDJJobEntity();
            ddjJobEntity.setPosition(Integer.parseInt(doorWay));
            if (taskType==1){
                ddjJobEntity.setType(1);
                ddjJobEntity.setTargetPosition(position);
                ddjJobEntity.setTakePosition(doorWay);
            }
            if (taskType==0){
                ddjJobEntity.setType(2);
                ddjJobEntity.setTargetPosition(doorWay);
                ddjJobEntity.setTakePosition(position);
            }
            //调用堆垛机,本次操作存入日志
            Integer status = ddjService.sendTaskAndListening(ddjJobEntity);
            System.err.println("============"+status+"===============");
            MtCcDeviceCommunLogEntity mtCcDeviceCommunLogEntity=new MtCcDeviceCommunLogEntity();
            mtCcDeviceCommunLogEntity.setCreateTime(new Date());
            mtCcDeviceCommunLogEntity.setStatus(0);
            mtCcDeviceCommunLogEntity.setContent("调用堆垛机，出入库:"+taskType+"起终点"+ddjJobEntity.getTargetPosition()+"-"+ddjJobEntity.getTakePosition()+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode());
            mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogEntity);
            if (status==-3){ //-3 代表 重置称重plc 失败
                MtCcDeviceCommunLogEntity mtCcDeviceCommunLogError=new MtCcDeviceCommunLogEntity();
                mtCcDeviceCommunLogError.setCreateTime(new Date());
                mtCcDeviceCommunLogError.setStatus(1);
                mtCcDeviceCommunLogError.setContent("调用堆垛机失败，IOServer写入异常本次任务重新执行，出入库:"+taskType+"起终点"+ddjJobEntity.getTargetPosition()+"-"+ddjJobEntity.getTakePosition()+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode());
                mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogError);
                status = ddjService.sendTaskAndListening(ddjJobEntity);//调用堆垛机,本次操作存入日志
                MtCcDeviceCommunLogEntity mtCcDeviceCommunLog=new MtCcDeviceCommunLogEntity();
                mtCcDeviceCommunLog.setCreateTime(new Date());
                mtCcDeviceCommunLog.setStatus(0);
                mtCcDeviceCommunLog.setContent("本次任务第二次调用堆垛机，出入库:"+taskType+"起终点"+ddjJobEntity.getTargetPosition()+"-"+ddjJobEntity.getTakePosition()+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode());
                mtCcDeviceCommunLogService.save(mtCcDeviceCommunLog);
            }
            if (status==-4){//-4 代表 出入库当前有障碍物,页面弹窗提醒,本次任务不执行,任务状态改为0;
                WebSocketServer webSocketServer1=new WebSocketServer();
                webSocketServer1.sendtoAll("调用堆垛机失败，出入库平台有货物，出入口:"+doorWay.substring(3,4));
                MtCcDeviceCommunLogEntity mtCcDeviceCommunLogError=new MtCcDeviceCommunLogEntity();
                mtCcDeviceCommunLogError.setCreateTime(new Date());
                mtCcDeviceCommunLogError.setStatus(1);
                mtCcDeviceCommunLogError.setContent("调用堆垛机失败，出入库平台有货物，出入库:"+taskType+"起终点"+ddjJobEntity.getTargetPosition()+"-"+ddjJobEntity.getTakePosition()+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode());
                mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogError);
                mtCcTaskRunInfoDetEntity.setStatus(0);
                mtCcTaskRunInfoDetService.updateById(mtCcTaskRunInfoDetEntity);
                webSocketServer.sendtoAll("本次出库操作对应平台存在障碍物,请移除平台上的障碍物后再执行本条子任务!");

                return R.error("本次出库操作对应平台存在障碍物,请移除平台上的障碍物后再执行本条子任务!");
            }
            if (status==1){//堆垛机执行成功,改变子任务状态，改变库位信息状态
                MtCcDeviceCommunLogEntity mtCcDeviceCommunLogSucess=new MtCcDeviceCommunLogEntity();
                mtCcDeviceCommunLogSucess.setCreateTime(new Date());
                mtCcDeviceCommunLogSucess.setStatus(1);
                mtCcDeviceCommunLogSucess.setContent("调用堆垛机执行任务成功，出入库:"+taskType+"起终点"+ddjJobEntity.getTargetPosition()+"-"+ddjJobEntity.getTakePosition()+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode());
                mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogSucess);

                mtCcTaskRunInfoDetEntity.setStatus(2);
                mtCcTaskRunInfoDetService.updateById(mtCcTaskRunInfoDetEntity);

                MtCcWarehousPositionRelatLogEntity mtCcWarehousPositionRelatLogEntity=new MtCcWarehousPositionRelatLogEntity();
                int lengthForCode = mtCcTaskRunInfoDetEntity.getModel().length();//编码长度
                int productNum=(lengthForCode+1)/10;//该库位上存放的产品有几种
                if (mtCcTaskRunInfoDetEntity.getTaskType()==0){//出库，库位信息删除，是否被占用改为0可用
                    for (int i = 0; i <productNum ; i++) {
                        String productCode = mtCcTaskRunInfoDetEntity.getModel().substring(10 * i, 10 * i + 9);
                        String[] strings = mtCcTaskRunInfoDetEntity.getNum().split("\\+");
                        String productQuantity=strings[i];//第i种货物的数量
                        MtCcProductEntity product = mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>().eq("product_code", productCode));
                        List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatService
                                .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                        .eq("warehous_position_id", warehousRankId)
                                        .eq("product_id", product.getId())
                                        .eq("status", 4));
                        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list
                             ) {
                            mtCcWarehousPositionRelatService.removeById(mtCcWarehousPositionRelatEntity);//根据库位id和产品id删除库位详情记录
                        }

                        //库位详情日志
                        mtCcWarehousPositionRelatLogEntity.setIsOutIn(0);
                        mtCcWarehousPositionRelatLogEntity.setCreateTime(new Date());
                        mtCcWarehousPositionRelatLogEntity.setProductId(product.getId());
                        mtCcWarehousPositionRelatLogEntity.setQuantity(Integer.parseInt(productQuantity));
                        mtCcWarehousPositionRelatLogEntity.setWarehousPositionId(warehousRankId);
                        mtCcWarehousPositionRelatLogService.save(mtCcWarehousPositionRelatLogEntity);
                    }
                        MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankService.getById(warehousRankId);
                        mtCcWarehousRankEntity.setIsOccupy(0);
                        mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新rank表状态
                    //查询本库位上不是预出库的货物,若存在,调用回库方法
                    List<MtCcWarehousPositionRelatEntity> list1 = mtCcWarehousPositionRelatService
                            .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                    .eq("warehous_position_id", warehousRankId)
                                    .eq("status", 2));
                    if (list1.size()!=0){
                        BackWarehous.backWarehous(list1,taskRunInfoServiceById);
                        isProducts=true;
                    }


                    /*MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankService.getById(warehousRankId);
                    mtCcWarehousRankEntity.setIsOccupy(0);
                    mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新rank表状态*/
                }else {//入库
                    for (int i = 0; i < productNum; i++) {
                        String productCode = mtCcTaskRunInfoDetEntity.getModel().substring(10 * i, 10 * i + 9);
                        String[] strings = mtCcTaskRunInfoDetEntity.getNum().split("\\+");
                        String productQuantity=strings[i];//第i种货物的数量
                        MtCcProductEntity product = mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>().eq("product_code", productCode));
                        List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatService
                                .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                        .eq("warehous_position_id", warehousRankId).eq("product_id", product.getId()));
                        MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankService.getById(warehousRankId);
                        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list
                             ) {
                            mtCcWarehousPositionRelatEntity.setStatus(2);
                            mtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelatEntity);
                        }/*else {//若有回库的货物，新插入货物对应的库位详情记录
                            MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity1=new MtCcWarehousPositionRelatEntity();
                            mtCcWarehousPositionRelatEntity1.setCreateTime(new Date());
                            mtCcWarehousPositionRelatEntity1.setStatus(2);
                            mtCcWarehousPositionRelatEntity1.setIsOccupy(1);
                            mtCcWarehousPositionRelatEntity1.setProductId(product.getId());
                            mtCcWarehousPositionRelatEntity1.setProductCode(product.getProductCode());
                            mtCcWarehousPositionRelatEntity1.setProductName(product.getProductName());
                            mtCcWarehousPositionRelatEntity1.setQuantity(Integer.parseInt(productQuantity));
                            mtCcWarehousPositionRelatEntity1.setWarehousPositionId(warehousRankId);
                            mtCcWarehousPositionRelatEntity1.setWarehousPositionName(mtCcWarehousRankEntity.getAnotherName());
                            mtCcWarehousPositionRelatService.save(mtCcWarehousPositionRelatEntity1);
                        }*/
                        //库位详情日志
                        mtCcWarehousPositionRelatLogEntity.setIsOutIn(1);
                        mtCcWarehousPositionRelatLogEntity.setCreateTime(new Date());
                        mtCcWarehousPositionRelatLogEntity.setProductId(product.getId());
                        mtCcWarehousPositionRelatLogEntity.setQuantity(Integer.parseInt(productQuantity));
                        mtCcWarehousPositionRelatLogEntity.setWarehousPositionId(warehousRankId);
                        mtCcWarehousPositionRelatLogService.save(mtCcWarehousPositionRelatLogEntity);
                    }
                }

                //新建一条出入库子任务存日志
                mtCcWarehousPositionRelatLogEntity.setIsOutIn(1);
                MtCcTaskRunInfoDetLogEntity mtCcTaskRunInfoDetLogEntity=new MtCcTaskRunInfoDetLogEntity();
                mtCcTaskRunInfoDetLogEntity.setCreateTime(mtCcTaskRunInfoDetEntity.getCreateTime());
                mtCcTaskRunInfoDetLogEntity.setWarehousRankId(mtCcTaskRunInfoDetEntity.getWarehousRankId());
                mtCcTaskRunInfoDetLogEntity.setWarehousRankName(mtCcTaskRunInfoDetEntity.getWarehousRankName());
                mtCcTaskRunInfoDetLogEntity.setTaskRunId(mtCcTaskRunInfoDetEntity.getTaskRunId());
                mtCcTaskRunInfoDetLogEntity.setTaskType(mtCcTaskRunInfoDetEntity.getTaskType());
                mtCcTaskRunInfoDetLogEntity.setStatus(mtCcTaskRunInfoDetEntity.getStatus());
                mtCcTaskRunInfoDetLogEntity.setTaskDetCode(mtCcTaskRunInfoDetEntity.getTaskDetCode());
                mtCcTaskRunInfoDetLogEntity.setModel(mtCcTaskRunInfoDetEntity.getModel());
                mtCcTaskRunInfoDetLogEntity.setNum(mtCcTaskRunInfoDetEntity.getNum());
                mtCcTaskRunInfoDetLogEntity.setUnit(mtCcTaskRunInfoDetEntity.getUnit());
                mtCcTaskRunInfoDetLogEntity.setIsHeight(mtCcTaskRunInfoDetEntity.getIsHeight());
                mtCcTaskRunInfoDetLogEntity.setIsWeight(mtCcTaskRunInfoDetEntity.getIsWeight());
                mtCcTaskRunInfoDetLogEntity.setRemarks(mtCcTaskRunInfoDetEntity.getRemarks());
                mtCcTaskRunInfoDetLogEntity.setDateNum(mtCcTaskRunInfoDetEntity.getDateNum());
                mtCcTaskRunInfoDetLogService.save(mtCcTaskRunInfoDetLogEntity);
                Float target_position=ioServerService.getValueByName("Target_Position").r4Val;
                Float take_position = ioServerService.getValueByName("Take_Position").r4Val;
                Boolean l_start = ioServerService.getValueByName("L_Start").bitVal;
                System.out.println("target_position"+target_position+"=====take_position"+take_position);
                /*if (!(target_position==0&&take_position==0&&l_start==false)){
                    //IOServer异常，写入日志，报告管理员处理
                    MtCcDeviceCommunLogEntity mtCcDeviceCommunLog=new MtCcDeviceCommunLogEntity();
                    mtCcDeviceCommunLog.setCreateTime(new Date());
                    mtCcDeviceCommunLog.setStatus(0);
                    mtCcDeviceCommunLog.setContent("堆垛机执行任务结束后重置变量失败，IOServer写入异常");
                    mtCcDeviceCommunLogService.save(mtCcDeviceCommunLog);
                }*/
            }
            else if (status==-2){//堆垛机异常，判断是否重置任务或手动完成任务
                MtCcDeviceCommunLogEntity mtCcDeviceCommunLogError=new MtCcDeviceCommunLogEntity();
                mtCcDeviceCommunLogError.setCreateTime(new Date());
                mtCcDeviceCommunLogError.setStatus(1);
                mtCcDeviceCommunLogError.setContent("调用堆垛机执行任务途中出现故障，出入库:"+taskType+"起终点"+ddjJobEntity.getTargetPosition()+"-"+ddjJobEntity.getTakePosition()+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode());
                mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogError);
                Boolean reset_task = ioServerService.getValueByName("Reset_task").bitVal;
                Boolean submit_task = ioServerService.getValueByName("Submit_task").bitVal;
                do {
                    reset_task = ioServerService.getValueByName("Reset_task").bitVal;
                    submit_task = ioServerService.getValueByName("Submit_task").bitVal;
                }while (!reset_task&&!submit_task);

                ioServerService.setTagValue(IOServerEnum.PC_Status.getCollectionPointName(),"88",1);
                System.err.println("任务"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"重置,");
                while (true){
                    Thread.sleep(100);
                    Integer deviceStatusTemp = ioServerService.getValueByName(IOServerEnum.Device_Status.getCollectionPointName()).i2Val.intValue();
                    if(deviceStatusTemp==1){
                        ioServerService.setTagValue(IOServerEnum.PC_Status.getCollectionPointName(), "0", 1);
                        break;
                    }else {
                        continue;
                    }
                }
                if (submit_task){//手动执行成功
                    logger.info("submit_task任务手动完成");
                    mtCcTaskRunInfoDetEntity.setStatus(2);
                    mtCcTaskRunInfoDetService.updateById(mtCcTaskRunInfoDetEntity);

                    MtCcWarehousPositionRelatLogEntity mtCcWarehousPositionRelatLogEntity=new MtCcWarehousPositionRelatLogEntity();
                    int lengthForCode = mtCcTaskRunInfoDetEntity.getModel().length();//编码长度
                    int productNum=(lengthForCode+1)/10;//该库位上存放的产品有几种
                    if (mtCcTaskRunInfoDetEntity.getTaskType()==0){//出库，库位信息删除，是否被占用改为0可用
                        for (int i = 0; i <productNum ; i++) {
                            String productCode = mtCcTaskRunInfoDetEntity.getModel().substring(10 * i, 10 * i + 9);
                            String[] strings = mtCcTaskRunInfoDetEntity.getNum().split("\\+");
                            String productQuantity=strings[i];//第i种货物的数量
                            MtCcProductEntity product = mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>().eq("product_code", productCode));
                            List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatService
                                    .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                            .eq("warehous_position_id", warehousRankId)
                                            .eq("product_id", product.getId())
                                            .in("status", 2, 4));
                            for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list
                            ) {
                                mtCcWarehousPositionRelatService.removeById(mtCcWarehousPositionRelatEntity);//根据库位id和产品id删除库位详情记录
                            }

                            //库位详情日志
                            mtCcWarehousPositionRelatLogEntity.setIsOutIn(0);
                            mtCcWarehousPositionRelatLogEntity.setCreateTime(new Date());
                            mtCcWarehousPositionRelatLogEntity.setProductId(product.getId());
                            mtCcWarehousPositionRelatLogEntity.setQuantity(Integer.parseInt(productQuantity));
                            mtCcWarehousPositionRelatLogEntity.setWarehousPositionId(warehousRankId);
                            mtCcWarehousPositionRelatLogService.save(mtCcWarehousPositionRelatLogEntity);
                        }
                            MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankService.getById(warehousRankId);
                            mtCcWarehousRankEntity.setIsOccupy(0);
                            mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新rank表状态

                        //查询本库位上不是预出库的货物,若存在,调用回库方法
                        List<MtCcWarehousPositionRelatEntity> list1 = mtCcWarehousPositionRelatService
                                .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                        .eq("warehous_position_id", warehousRankId)
                                        .eq("status", 2));
                        if (list1.size()!=0){
                            BackWarehous.backWarehous(list1,taskRunInfoServiceById);
                            isProducts=true;
                        }

                    /*MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankService.getById(warehousRankId);
                    mtCcWarehousRankEntity.setIsOccupy(0);
                    mtCcWarehousRankService.updateById(mtCcWarehousRankEntity);//更新rank表状态*/
                    }else {//入库
                        for (int i = 0; i < productNum; i++) {
                            String productCode = mtCcTaskRunInfoDetEntity.getModel().substring(10 * i, 10 * i + 9);
                            String[] strings = mtCcTaskRunInfoDetEntity.getNum().split("\\+");
                            String productQuantity=strings[i];//第i种货物的数量
                            MtCcProductEntity product = mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>().eq("product_code", productCode));
                            List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatService
                                    .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                            .eq("warehous_position_id", warehousRankId).eq("product_id", product.getId()));
                            MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankService.getById(warehousRankId);
                            for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list
                            ) {
                                mtCcWarehousPositionRelatEntity.setStatus(2);
                                mtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelatEntity);
                            }/*else {//若有回库的货物，新插入货物对应的库位详情记录
                            MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity1=new MtCcWarehousPositionRelatEntity();
                            mtCcWarehousPositionRelatEntity1.setCreateTime(new Date());
                            mtCcWarehousPositionRelatEntity1.setStatus(2);
                            mtCcWarehousPositionRelatEntity1.setIsOccupy(1);
                            mtCcWarehousPositionRelatEntity1.setProductId(product.getId());
                            mtCcWarehousPositionRelatEntity1.setProductCode(product.getProductCode());
                            mtCcWarehousPositionRelatEntity1.setProductName(product.getProductName());
                            mtCcWarehousPositionRelatEntity1.setQuantity(Integer.parseInt(productQuantity));
                            mtCcWarehousPositionRelatEntity1.setWarehousPositionId(warehousRankId);
                            mtCcWarehousPositionRelatEntity1.setWarehousPositionName(mtCcWarehousRankEntity.getAnotherName());
                            mtCcWarehousPositionRelatService.save(mtCcWarehousPositionRelatEntity1);
                        }*/
                            //库位详情日志
                            mtCcWarehousPositionRelatLogEntity.setIsOutIn(1);
                            mtCcWarehousPositionRelatLogEntity.setCreateTime(new Date());
                            mtCcWarehousPositionRelatLogEntity.setProductId(product.getId());
                            mtCcWarehousPositionRelatLogEntity.setQuantity(Integer.parseInt(productQuantity));
                            mtCcWarehousPositionRelatLogEntity.setWarehousPositionId(warehousRankId);
                            mtCcWarehousPositionRelatLogService.save(mtCcWarehousPositionRelatLogEntity);
                        }
                    }

                    //新建一条出入库子任务存日志
                    mtCcWarehousPositionRelatLogEntity.setIsOutIn(1);
                    MtCcTaskRunInfoDetLogEntity mtCcTaskRunInfoDetLogEntity=new MtCcTaskRunInfoDetLogEntity();
                    mtCcTaskRunInfoDetLogEntity.setCreateTime(mtCcTaskRunInfoDetEntity.getCreateTime());
                    mtCcTaskRunInfoDetLogEntity.setWarehousRankId(mtCcTaskRunInfoDetEntity.getWarehousRankId());
                    mtCcTaskRunInfoDetLogEntity.setWarehousRankName(mtCcTaskRunInfoDetEntity.getWarehousRankName());
                    mtCcTaskRunInfoDetLogEntity.setTaskRunId(mtCcTaskRunInfoDetEntity.getTaskRunId());
                    mtCcTaskRunInfoDetLogEntity.setTaskType(mtCcTaskRunInfoDetEntity.getTaskType());
                    mtCcTaskRunInfoDetLogEntity.setStatus(mtCcTaskRunInfoDetEntity.getStatus());
                    mtCcTaskRunInfoDetLogEntity.setTaskDetCode(mtCcTaskRunInfoDetEntity.getTaskDetCode());
                    mtCcTaskRunInfoDetLogEntity.setModel(mtCcTaskRunInfoDetEntity.getModel());
                    mtCcTaskRunInfoDetLogEntity.setNum(mtCcTaskRunInfoDetEntity.getNum());
                    mtCcTaskRunInfoDetLogEntity.setUnit(mtCcTaskRunInfoDetEntity.getUnit());
                    mtCcTaskRunInfoDetLogEntity.setIsHeight(mtCcTaskRunInfoDetEntity.getIsHeight());
                    mtCcTaskRunInfoDetLogEntity.setIsWeight(mtCcTaskRunInfoDetEntity.getIsWeight());
                    mtCcTaskRunInfoDetLogEntity.setRemarks(mtCcTaskRunInfoDetEntity.getRemarks());
                    mtCcTaskRunInfoDetLogEntity.setDateNum(mtCcTaskRunInfoDetEntity.getDateNum());
                    mtCcTaskRunInfoDetLogService.save(mtCcTaskRunInfoDetLogEntity);
                }else {
                    mtCcTaskRunInfoDetEntity.setStatus(0);
                    mtCcTaskRunInfoDetService.updateById(mtCcTaskRunInfoDetEntity);
                    System.err.println("任务编码为"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"的子任务收到堆垛机重置任务.状态已重置!");
                    logger.info("任务编码为"+mtCcTaskRunInfoDetEntity.getTaskDetCode()+"的子任务收到堆垛机重置任务.状态已重置!");
                    return R.error("堆垛机排除异常后任务重置,请重新发起执行任务!");
                }
                        /*else {//重置变量
                            ioServerService.setTagValue(IOServerEnum.PC_Status.getCollectionPointName(),"88",1);
                        }*/

            }else {//堆垛机执行任务异常
                MtCcDeviceCommunLogEntity mtCcDeviceCommunLogEntity1=new MtCcDeviceCommunLogEntity();
                mtCcDeviceCommunLogEntity1.setCreateTime(new Date());
                mtCcDeviceCommunLogEntity1.setStatus(0);
                mtCcDeviceCommunLogEntity1.setContent("堆垛机执行任务过程中出现未知异常,出入库："+taskType+"起终点"+ddjJobEntity.getTargetPosition()+"-"+ddjJobEntity.getTakePosition()+"任务编码"+mtCcTaskRunInfoDetEntity.getTaskDetCode());
                mtCcDeviceCommunLogService.save(mtCcDeviceCommunLogEntity1);

                mtCcTaskRunInfoDetEntity.setStatus(0);
                mtCcTaskRunInfoDetService.updateById(mtCcTaskRunInfoDetEntity);
                return R.error("堆垛机执行任务过程中出现未知异常,请排除故障后重试!");
            }
            //查询该条子任务对应的主任务下是否存在未执行的子任务
            int count1 = mtCcTaskRunInfoDetService.count(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                    .eq("status", 0)
                    .eq("task_run_id", mtCcTaskRunInfoDetEntity.getTaskRunId()));
            MtCcTaskRunInfoEntity id2 = mtCcTaskRunInfoService.getOne(new QueryWrapper<MtCcTaskRunInfoEntity>()
                    .eq("id", mtCcTaskRunInfoDetEntity.getTaskRunId()));
            Integer type = id2.getTaskType();
            if (count1==0) {//不存在未执行的子任务,更改主任务状态为执行完毕

                taskRunInfoServiceById.setStatus(2);
                mtCcTaskRunInfoService.updateById(taskRunInfoServiceById);
                //出库主任务回库选项
                if (type == 0){
                    String dateNum = null;
                    int allNum = Integer.parseInt(taskRunInfoServiceById.getNum());//主任务中的总数量.
                    List<MtCcTaskRunInfoDetEntity> allTaskInfoDets = mtCcTaskRunInfoDetService
                            .list(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                                    .eq("task_run_id", taskRunInfoServiceById.getId()));//该主任务下的所有子任务
                    int num = 0;
                    for (MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity1 : allTaskInfoDets
                    ) {
                        num = num + Integer.parseInt(mtCcTaskRunInfoDetEntity1.getNum());
                        dateNum = mtCcTaskRunInfoDetEntity1.getDateNum();
                    }
                    if (allNum < num) {//出库的数量大于输入的数量
                        int quantity = num - allNum;
                        //如果多物料回库已经触发.
                        if(isProducts){
                            //多物料回库的库位,
                            Integer warehousRankId1 = mtCcTaskRunInfoDetEntity.getWarehousRankId();
                            //查出那条多物料回库子任务.
                            MtCcTaskRunInfoDetEntity one = mtCcTaskRunInfoDetService.getOne(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                                    .eq("warehous_rank_id", warehousRankId1)
                                    .eq("status", 0));
                            one.setModel(one.getModel().concat(taskRunInfoServiceById.getModel()));
                            one.setDateNum(one.getDateNum().concat(taskRunInfoServiceById.getDateNum()));
                            if (taskRunInfoServiceById.getRemarks()==null){
                                taskRunInfoServiceById.setRemarks("");
                            }
                            one.setRemarks(one.getRemarks().concat(taskRunInfoServiceById.getRemarks()));
                            one.setNum(one.getNum().concat(String.valueOf(quantity)));
                            mtCcTaskRunInfoDetService.updateById(one);
                            List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatService
                                    .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                    .eq("warehous_position_id", warehousRankId1)
                                            .eq("status", 3));
                                MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity1 = list.get(0);
                                mtCcWarehousPositionRelatEntity1.setProductCode(taskRunInfoServiceById.getModel());
                                mtCcWarehousPositionRelatEntity1.setProductId(taskRunInfoServiceById.getProductId());
                                mtCcWarehousPositionRelatEntity1.setProductName(mtCcProductService.getById(taskRunInfoServiceById.getProductId()).getProductName());
                                mtCcWarehousPositionRelatEntity1.setQuantity(Integer.parseInt(String.valueOf(quantity)));
                                mtCcWarehousPositionRelatEntity1.setDateNum(taskRunInfoServiceById.getDateNum());
                                mtCcWarehousPositionRelatEntity1.setRemark(taskRunInfoServiceById.getRemarks());
                                mtCcWarehousPositionRelatService.save(mtCcWarehousPositionRelatEntity1);

                        }else {//普通回库
                            MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity = new MtCcTaskRunInfoEntity();
                            mtCcTaskRunInfoEntity.setCreateTime(taskRunInfoServiceById.getCreateTime());
                            mtCcTaskRunInfoEntity.setUserId(taskRunInfoServiceById.getUserId());
                            mtCcTaskRunInfoEntity.setUserName(taskRunInfoServiceById.getUserName());
                            mtCcTaskRunInfoEntity.setSource(taskRunInfoServiceById.getSource());
                            mtCcTaskRunInfoEntity.setTaskType(1);
                            mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod
                                    .creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE, taskRunInfoServiceById.getTaskCode().substring(1, 3)));
                            mtCcTaskRunInfoEntity.setStatus(0);
                            mtCcTaskRunInfoEntity.setProductId(taskRunInfoServiceById.getProductId());
                            mtCcTaskRunInfoEntity.setModel(taskRunInfoServiceById.getModel());
                            mtCcTaskRunInfoEntity.setNum(String.valueOf(quantity));
                            mtCcTaskRunInfoEntity.setUnit(taskRunInfoServiceById.getUnit());
                            mtCcTaskRunInfoEntity.setFBillno(taskRunInfoServiceById.getFBillno());
                            mtCcTaskRunInfoEntity.setFEntryId(taskRunInfoServiceById.getFEntryId());
                            mtCcTaskRunInfoEntity.setFBatchNo(taskRunInfoServiceById.getFBatchNo());
                            mtCcTaskRunInfoEntity.setRemarks(taskRunInfoServiceById.getRemarks());
                            mtCcTaskRunInfoEntity.setDateNum(dateNum);
                            mtCcTaskRunInfoService.save(mtCcTaskRunInfoEntity);//新建一条剩余货物数量的主任务;
                            MtCcTaskRunInfoEntity taskInfo = mtCcTaskRunInfoService
                                    .getOne(new QueryWrapper<MtCcTaskRunInfoEntity>()
                                            .eq("task_code", mtCcTaskRunInfoEntity.getTaskCode()));
                            MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity1 = new MtCcTaskRunInfoDetEntity();
                            mtCcTaskRunInfoDetEntity1.setCreateTime(taskRunInfoServiceById.getCreateTime());
                            mtCcTaskRunInfoDetEntity1.setWarehousRankId(mtCcTaskRunInfoDetEntity.getWarehousRankId());
                            mtCcTaskRunInfoDetEntity1.setTaskType(taskInfo.getTaskType());
                            mtCcTaskRunInfoDetEntity1.setTaskDetCode(mtCcTaskRunInfoEntity.getTaskCode());
                            mtCcTaskRunInfoDetEntity1.setTaskRunId(taskInfo.getId());
                            mtCcTaskRunInfoDetEntity1.setStatus(0);
                            mtCcTaskRunInfoDetEntity1.setModel(mtCcTaskRunInfoEntity.getModel());
                            mtCcTaskRunInfoDetEntity1.setNum(mtCcTaskRunInfoEntity.getNum());
                            mtCcTaskRunInfoDetEntity1.setDateNum(mtCcTaskRunInfoEntity.getDateNum());
                            mtCcTaskRunInfoDetEntity1.setRemarks(mtCcTaskRunInfoEntity.getRemarks());
                            mtCcTaskRunInfoDetEntity1.setUnit(mtCcTaskRunInfoEntity.getUnit());
                            mtCcTaskRunInfoDetEntity1.setWarehousRankName(mtCcWarehousRankService.getById(mtCcTaskRunInfoDetEntity.getWarehousRankId()).getAnotherName());
                            mtCcTaskRunInfoDetService.save(mtCcTaskRunInfoDetEntity1);//新建子任务
                            MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity = new MtCcWarehousPositionRelatEntity();
                            mtCcWarehousPositionRelatEntity.setCreateTime(taskRunInfoServiceById.getCreateTime());
                            mtCcWarehousPositionRelatEntity.setStatus(3);
                            mtCcWarehousPositionRelatEntity.setIsOccupy(1);
                            mtCcWarehousPositionRelatEntity.setIsEmpty(0);
                            mtCcWarehousPositionRelatEntity.setProductCode(mtCcTaskRunInfoEntity.getModel());
                            mtCcWarehousPositionRelatEntity.setProductId(taskRunInfoServiceById.getProductId());
                            mtCcWarehousPositionRelatEntity.setProductName(mtCcProductService.getById(taskRunInfoServiceById.getProductId()).getProductName());
                            mtCcWarehousPositionRelatEntity.setQuantity(Integer.parseInt(mtCcTaskRunInfoEntity.getNum()));
                            mtCcWarehousPositionRelatEntity.setWarehousPositionId(mtCcTaskRunInfoDetEntity.getWarehousRankId());
                            mtCcWarehousPositionRelatEntity.setWarehousPositionName(mtCcTaskRunInfoDetEntity1.getWarehousRankName());
                            mtCcWarehousPositionRelatEntity.setDateNum(mtCcTaskRunInfoEntity.getDateNum());
                            mtCcWarehousPositionRelatEntity.setRemark(mtCcTaskRunInfoEntity.getRemarks());
                            mtCcWarehousPositionRelatService.save(mtCcWarehousPositionRelatEntity);//添加货物预入库
                            MtCcWarehousRankEntity id1 = mtCcWarehousRankService.getOne(new QueryWrapper<MtCcWarehousRankEntity>().eq("id", mtCcTaskRunInfoDetEntity.getWarehousRankId()));
                            id1.setIsOccupy(1);
                            mtCcWarehousRankService.updateById(id1);

                        }
                    }
                }
                    return R.ok("该条主任务已执行完毕!");
            }
            return R.ok("该条子任务执行成功!");
        }
    }
}
